#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using Agresso.Interface.Layer.Domain;
using System.Web;
using Agresso.Interface.Layer.Service;
using System.Collections;

#endregion

namespace Agresso.Interface.Layer.Web
{
    public partial class UserForm : Form
    {
        User m_user;
        User m_current_user;
        EditMode m_editmode;
        UserService m_userService;
        MainForm m_mainForm;
        UserControlManageUser m_parentForm;

        public UserForm(string name, User user, User currentUser, MainForm mainForm, UserControlManageUser parentForm, EditMode editmode)
        {
            InitializeComponent();
            m_user = user;
            m_current_user = currentUser;
            m_mainForm = mainForm;
            this.Name = name;
            m_parentForm = parentForm;
            m_userService = (UserService)ServiceFactory.Instance().GetService(ServiceFactory.USERSERVICE);
            initButtonEvent();
            initComboDataSource();
            if (user.UserID == 0)
            {
                clearForm();
            }
            else
            {
                loadUser(); 
                enableForm(editmode == EditMode.Edit);
            }
            setEditMode(editmode);
            //enableForm(false);
           
        }

        private void clearForm()
        {
            textBoxUserName.Text = string.Empty;
            textBoxUserCode.Text = string.Empty;
            textBoxPassword.Text = string.Empty;
            textBoxConfirmPassword.Text = string.Empty;
            textBoxEmail.Text = string.Empty;
            dateTimePickerExpiry.Value = DateTime.Today.AddMonths(3);
            checkBoxPasswordExpired.Checked = true;
            //comboBoxRoleMaster.SelectedIndex = 0;
            m_user = new User();
            // listViewRecords.SelectedItems.Clear();
            clearErrorProvider();
            enableForm(true);
            setEditMode(EditMode.Create);
            textBoxUserCode.SelectAll();
        }

        private void initButtonEvent()
        {
            //toolBarButtonClear.Click += new EventHandler(toolBarButtonClear_Click);
            toolBarButtonSave.Click += new EventHandler(toolBarButtonSave_Click);
            //toolBarButtonDelete.Click += new EventHandler(toolBarButtonDelete_Click);
            toolBarButtonRefresh.Click += new EventHandler(toolBarButtonRefresh_Click);
            toolBarButtonEdit.Click += new EventHandler(toolBarButtonEdit_Click);
            //toolBarButtonSearch.Click += new EventHandler(toolBarButtonSearch_Click);
            toolBarButtonCancel.Click += new EventHandler(toolBarButtonCancel_Click);
        }

        void toolBarButtonEdit_Click(object sender, EventArgs e)
        {
            setEditMode(EditMode.Edit);
            enableForm(!toolBarButtonEdit.Enabled);
        }

        void toolBarButtonCancel_Click(object sender, EventArgs e)
        {
            if (m_editmode == EditMode.Load)
                this.Close();
            else
            MessageBox.Show("Are you sure to cancel?", "Confirmation", MessageBoxButtons.YesNo,
               MessageBoxIcon.Question, new EventHandler(cancelConfirmation));
        }

        void cancelConfirmation(object sender, EventArgs e)
        {
             MessageBoxWindow msg = (MessageBoxWindow)sender;
             if (msg.DialogResult == DialogResult.Yes)
             {
                 this.Close();
             }
        }
        private void initComboDataSource()
        {
           // IList rolemasters = m_userService.LoadAllRoleMaster();
           // comboBoxRoleMaster.DataSource = rolemasters;
        }

        private IList GetDataGridRoleMasterComboValues()
        {
            IList rolemasters = m_userService.LoadAllRoleMaster();
            IList cbvals = new ArrayList();
            foreach (RoleMaster m in rolemasters)
            {
                cbvals.Add(m.RoleName);
            }
            return cbvals;
        }

        private RoleMaster GetRoleMasterByName(string name)
        {
            IList rolemasters = m_userService.LoadAllRoleMaster();
            RoleMaster result = null;
            foreach (RoleMaster m in rolemasters)
            {
                if (m.RoleName == name)
                {
                    result = m;
                    break;
                }
            }
            return result;
        }

        void toolBarButtonRefresh_Click(object sender, EventArgs e)
        {
            //loadAllUser();
            initComboDataSource();
            if (m_editmode == EditMode.Load || m_editmode == EditMode.Edit)
                loadUser();
        }

        private void clearErrorProvider()
        {
            errorProviderUser.SetError(textBoxUserCode, "");
            errorProviderUser.SetError(textBoxUserName, "");
            errorProviderUser.SetError(textBoxPassword, "");
            errorProviderUser.SetError(textBoxConfirmPassword, "");
            //errorProviderUser.SetError(comboBoxRoleMaster, "");
        }
        private bool isValidForm()
        {
            clearErrorProvider();
            bool valid_a = textBoxUserCode.Text != string.Empty;
            bool valid_b = textBoxUserName.Text != string.Empty;
            bool valid_c = textBoxPassword.Text != string.Empty;
            bool valid_d = textBoxPassword.Text == textBoxConfirmPassword.Text;
            //bool valid_e = comboBoxRoleMaster.SelectedItem != null;
            if (!valid_a) errorProviderUser.SetError(textBoxUserCode, "User Code can not be empty");
            if (!valid_b) errorProviderUser.SetError(textBoxUserName, "User Name can not be empty");
            if (!valid_c) errorProviderUser.SetError(textBoxPassword, "Password can not be empty");
            if (!valid_d) errorProviderUser.SetError(textBoxConfirmPassword, "Password is different");
           // if (!valid_e) errorProviderUser.SetError(comboBoxRoleMaster, "Role can not be empty");
            return valid_a && valid_b && valid_c && valid_d;// && valid_e;
        }

        void toolBarButtonSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (isValidForm())
                {
                    m_user.UserCode = textBoxUserCode.Text;
                    m_user.UserName = textBoxUserName.Text;
                    m_user.Password = textBoxPassword.Text;
                    m_user.ActiveCode = checkBoxActive.Checked;
                    m_user.EmailID = textBoxEmail.Text;
                    m_user.ExpiryDate = dateTimePickerExpiry.Value;
                    m_user.PasswordExpired = checkBoxPasswordExpired.Checked;
                    //m_user.Role = (RoleMaster)comboBoxRoleMaster.SelectedItem;
                    m_user.CreatenBy = m_current_user.UserID;
                    m_user.ModificationBy = m_current_user.UserID;
                    m_user.CrewPayrollAccess =checkBoxCrewPayroll.Checked;
                    m_user.DADeskAccess = checkBoxDADesk.Checked;
                    m_user.SoftmarAccess = checkBoxSoftmar.Checked;
                    m_user.WinSDSAccess = checkBoxWinSDS.Checked;
                    m_user.AdminAccess = checkBoxAdminsitrator.Checked;
                    m_user.Alert = checkBoxAlert.Checked;
                    m_user.SendAlertToEmail = checkBoxAlertSendToEmail.Checked;
                    m_user.ModificationTerminal = HttpContext.Current.User.Identity.Name.ToString() + "," + HttpContext.Current.Request.UserHostAddress;
                    if (m_user.UserID > 0)
                    {
                        m_userService.UpdateUser(m_user);
                    }
                    else
                    {
                        m_userService.InsertUser(m_user);
                    }
                    if (m_user.UserID == m_current_user.UserID)
                    {
                        m_mainForm.RefreshUserAndPermission(m_user);
                    }
                    m_parentForm.LoadAllUser();
                  //  MessageBox.Show("Record has been saved.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                    MessageBox.Show("Record has been saved", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, new EventHandler(closeForm));
                    //clearForm();
                    //loadAllUser();
                    //setEditMode(EditMode.Saved);
                   // this.Close();
                }
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }

        }

        void closeForm(object sender, EventArgs e)
        {
            this.Close();
        }

        private void setEditMode(EditMode editMode)
        {
            toolBarButtonSave.Enabled = (editMode == EditMode.Create || editMode == EditMode.Edit || editMode == EditMode.Saved || editMode == EditMode.Delete) && m_current_user.AdminAccess;//RoleDetails(this.Name).NewFlag;
           // toolBarButtonDelete.Enabled = (editMode == EditMode.Loaded) && m_current_user.RoleDetails(this.Name).DeleteFlag;
            toolBarButtonEdit.Enabled = (editMode == EditMode.Load) && m_current_user.AdminAccess;//RoleDetails(this.Name).UpdateFlag;
            //toolBarButtonClear.Enabled = m_current_user.RoleDetails(this.Name).UpdateFlag;
            m_editmode = editMode;
        }

        private void enableForm(bool enable)
        {
            textBoxUserName.ReadOnly = !enable;
            textBoxUserCode.ReadOnly = !enable;
            textBoxPassword.ReadOnly = !enable;
            textBoxConfirmPassword.ReadOnly = !enable;
            checkBoxActive.Enabled = enable;
            textBoxEmail.ReadOnly = !enable;
            dateTimePickerExpiry.Enabled = enable;
            //comboBoxRoleMaster.Enabled = enable;
            checkBoxPasswordExpired.Enabled = enable;
            checkBoxWinSDS.Enabled = enable;
            checkBoxSoftmar.Enabled = enable;
            checkBoxDADesk.Enabled = enable;
            checkBoxCrewPayroll.Enabled = enable;
            checkBoxAdminsitrator.Enabled = enable;
            checkBoxAlertSendToEmail.Enabled = enable;
            checkBoxAlert.Enabled = enable;
        }

        private void loadUser()
        {
            //m_user.Role = m_userService.GetRoleMasterByID(m_user.Role.RoleMasterID);
            textBoxUserName.Text = m_user.UserName;
            textBoxUserCode.Text = m_user.UserCode;
            textBoxPassword.Text = m_user.Password;
            textBoxConfirmPassword.Text = m_user.Password;
            textBoxEmail.Text = m_user.EmailID;
            checkBoxActive.Checked = m_user.ActiveCode;
            dateTimePickerExpiry.Value = m_user.ExpiryDate;
            checkBoxPasswordExpired.Checked = m_user.PasswordExpired;
            //comboBoxRoleMaster.Text = m_user.Role.ToString();
            checkBoxCrewPayroll.Checked = m_user.CrewPayrollAccess;
            checkBoxDADesk.Checked = m_user.DADeskAccess;
            checkBoxSoftmar.Checked = m_user.SoftmarAccess;
            checkBoxWinSDS.Checked = m_user.WinSDSAccess;
            checkBoxAdminsitrator.Checked = m_user.AdminAccess;
            checkBoxAlert.Checked = m_user.Alert;
            checkBoxAlertSendToEmail.Checked = m_user.SendAlertToEmail;
        }
    }
}